<?php
namespace Client\Controller;

use Zend\Mvc\Controller\AbstractActionController;

class CommonController extends AbstractActionController
{
 
    protected $catalogTable;
    protected $typeTable;
    protected $bannerTable;
    protected $organizationTable;
    protected $categoryTable;
    protected $productTable;
    protected $materialTable;
    protected $favoritesTable;
    protected $properties;
    protected $adapter;
    protected $configSys;
    
    function __call($function_name, $args)
    {
        try {
            $tableName=str_replace('get','',$function_name);
            $path=sprintf("Client\Table\%1s",$tableName);
            return $this->getTable($this->properties[$tableName],$path);
        } catch (\Exception $e) {
            return parent::__call($function_name, $args);
        }
    }
    
    protected function getConfigSys()
    {
        if (!$this->configSys) {
    
            $config = $this->getServiceLocator()->get('Config');
            $this->configSys = $config['sys'];
        }
        return $this->configSys;
    }
    
    protected function getAdapter()
    {
        if (!$this->adapter) {
            $this->adapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
        }
        return $this->adapter;
    }
    
    protected function getCatalogTable()
    {
        return $this->getTable($this->catalogTable,'Client\Table\CatalogTable');
    }
    protected function getOrganizationTable()
    {
        return $this->getTable($this->organizationTable,'Client\Table\OrganizationTable');
    }
    protected function getCategoryTable()
    {
        return $this->getTable($this->categoryTable,'Client\Table\CategoryTable');
    }
    protected function getProductTable()
    {
        return $this->getTable($this->productTable,'Client\Table\ProductTable');
    }
    
    protected function getTypeTable()
    {
        return $this->getTable($this->typeTable,'Client\Table\TypeTable');
    }
    
    protected function getBannerTable()
    {
        return $this->getTable($this->bannerTable,'Client\Table\BannerTable');
    }
    
    protected function getMaterialTable()
    {
        return $this->getTable($this->materialTable,'Client\Table\MaterialTable');
    }
    
    protected function getFavoritesTable()
    {
        return $this->getTable($this->favoritesTable,'Client\Table\FavoritesTable');
    }
    
    
    private function getTable($table_name,$table_path)
    {
        if (!$table_name) {
            $sm = $this->getServiceLocator();
            $table_name = $sm->get($table_path);
        }
        return $table_name;
    }
    
    protected function getPaginatorInfo($paginator)
    {
        $set = array();
        $set['pagesInRange'] = $paginator->getPagesInRange($paginator->getCurrentPageNumber()-2,$paginator->getCurrentPageNumber()+2);
        $set['getTotalItemCount'] = $paginator->getTotalItemCount();
        $set['current'] = $paginator->getCurrentPageNumber();
        return $set;
    }
    
    protected function paginatorToArray($paginator,$callback = null)
    {
        $data = array();
        foreach ($paginator->getCurrentItems() as $item) {
            if ($callback != null) {
                $callback($item);
            }
            array_push($data, $item);
        }
        return $data;
    }
}